package com.zr.subClass.controller;

import com.zr.subClass.model.*;
import com.zr.subClass.model.PageSelectVo;
import com.zr.subClass.model.StatusVo;
import com.zr.subClass.service.SubClassService;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 天赐 on 2019/12/20.
 */
@RestController
@CrossOrigin//支持跨域
public class SubClassController {

    @Autowired
    private SubClassService subClassService;

    /*添加*/
    @PostMapping("subClass/add")
    public ResultVo addPlanClass(@RequestBody @Valid SubClassVo subClassVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }

        ResultVo resultVo = subClassService.add(subClassVo);
        return resultVo;
    }
    /*通过id获取值*/
    @GetMapping("s")
    public ResultVo queryById(Integer id){
        if (id == null){
            return ResultVoBuilder.error("id不能为空");
        }
        SubClassAllVo subClassAllVo = subClassService.queryById(id);
        return ResultVoBuilder.success(subClassAllVo);
    }
    /*修改信息*/
    @PostMapping("subClass/update")
    public ResultVo updatePlanClass(@RequestBody @Valid SubClassVo subClassVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }
        SubClassAllVo subClassAllVo = subClassService.queryById(subClassVo.getId());
        if (!subClassAllVo.getStatus()){

            return subClassService.update(subClassVo);
        }
        return ResultVoBuilder.error("启用状态不能修改");
    }
    /*通过id获取信息*/
    @PostMapping("subClass/updateStatus")
    @Transactional
    public ResultVo queryByIdStatus(@RequestBody @Valid SubStutas subStutas){

        ResultVo resultVo = subClassService.updateStudas(subStutas);
        return ResultVoBuilder.success(resultVo);
    }


    /*分页,多条件*/
    @PostMapping("subClass/queryPage")
    public ResultVo queryPage(@RequestBody PageSelectVo pageSelectVo){

        if (pageSelectVo.getStatusName()!=null){
            if(pageSelectVo.getStatus().equals("禁用")){
                pageSelectVo.setStatus(false);
            }else if (pageSelectVo.getStatus().equals("启用")){
                pageSelectVo.setStatus(true);
            }
        }

        ResultVo resultVo = subClassService.getPage(pageSelectVo);


        return resultVo;
    }


    @PostMapping("metadata/getPullDown")
    public ResultVo xiala(){
        List<StatusVo> statusVoList = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel("禁用");
        statusVo.setValue(0);
        statusVoList.add(statusVo);

        StatusVo statusVo2 = new StatusVo();
        statusVo2.setLabel("启用");
        statusVo2.setValue(1);
        statusVoList.add(statusVo2);

        StatusVo statusVo3 = new StatusVo();
        statusVo3.setLabel("全部");
        statusVo3.setValue(10);
        statusVoList.add(statusVo3);
        return ResultVoBuilder.success(statusVoList);
    }
}
